/* TABLE 2 and Appendix Table 3 IN COSAR, GRIECO AND TINTELNOT (2014) 
Inputs: 
- german_data_allyears.dta
- danish_data_allyears.dta
- german_data_9596.dta
- danish_data_9596.dta
Outputs: 
- table2.txt, table 2 in the paper
- tableA3.txt , Appendix table 3
*/

clear
/* global path "C:\Dropbox\Research\Finished or retired projects\CGT_WindBorder" */
global path "C:\Users\Felix Tintelnot\Dropbox\WindBorder\RestatCGTReplication"

*use "$path\Data\output_files\danish_data_9596.dta" 
*append using "$path\Data\output_files\german_data_9596.dta"

use "$path\Data\output_files\danish_data_allyears.dta",clear
append using "$path\Data\output_files\german_data_allyears.dta"

keep if year==1995 | year==1996
gen country = 1
replace country=0 if denmark==1
keep prod year distance_border country prodc_denmark 
replace distance_border = -distance_border if country==0 // in RDD, Denmark is to the left.
save tempRDD.dta,replace

// Column I: Baseline 1995-1996 RDD regression
gen distance_border2=distance_border^2
gen distance_border3=distance_border^3
gen interact = distance_border*country
gen interact2 = distance_border2*country
gen interact3 = distance_border3*country

qui regress prodc_denmark distance_border* interact* country, level(95)
estimates store m1
estadd local Data "1995-1996" , replace

// Column II: Entire sample with time trend
use "$path\Data\output_files\danish_data_allyears.dta",clear
append using "$path\Data\output_files\german_data_allyears.dta"

gen country = 1
replace country=0 if denmark==1
keep prod year distance_border country prodc_denmark 
replace distance_border = -distance_border if country==0 // in RDD, Denmark is to the left.

gen samplelb=1981
keep if year>samplelb //& year<2004
drop if distance>1000 // there is a project with distance 6000 miles! some mistake in coordinates?

gen trend = year - samplelb
gen bordertrend = country*trend 
qui tab year,gen(yeardum)

gen distance_border2=distance_border^2
gen distance_border3=distance_border^3
gen interact = distance_border*country
gen interact2 = distance_border2*country
gen interact3 = distance_border3*country

qui regress prodc_denmark distance_border* interact* country bordertrend trend yeardum*, level(95) //bordertrend yeardum*
estimates store m2
estadd local Data "1982-1995" , replace

// Output
esttab m1 m2, keep(country bordertrend ) coeflabels(country "Germany" bordertrend "Time trend") s(Data N r2) nomtitles nonumbers nostar nodepvars se 
esttab m1 m2 using "$path\SummaryStatistics\table2.txt", replace compress keep(country bordertrend ) coeflabels(country "Germany" bordertrend "Time trend") s(Data N r2) nomtitles nonumbers nostar nodepvars se 

/********************
Table Appendix 3 
********************/
use tempRDD.dta,clear

gen distance_border2=distance_border^2
gen distance_border3=distance_border^3
gen interact = distance_border*country
gen interact2 = distance_border2*country
gen interact3 = distance_border3*country

// BASELINE SPECIFICATION (CUBIC POLYNOMIAL WITH INTERACTIONS)//
regress prodc_denmark country distance_border* interact*, level(95) 
estimates store Baseline

// NON-LINEAR WITH NO INTERACTIONS (CUBIC POLYNOMIAL) //
regress prodc_denmark country distance_border*, level(95)
estimates store CubicNoInter

// LINEAR SPECIFICATION //
regress prodc_denmark country distance_border interact, level(95)
estimates store Linear

// LINEAR SPECIFICATION WITH NO INTERACTION //
regress prodc_denmark distance_border country, level(95)
estimates store LinNoInter

esttab Baseline CubicNoInter Linear LinNoInter, keep(country distance_border distance_border2 distance_border3 interact interact2 interact3) ///
coeflabels(country "Germany" distance_border "alfa1" distance_border2 "alfa2" distance_border3 "alfa3" interact "eta1" interact2 "eta2" interact3 "eta3") ///
s(N r2) nonumbers nodepvars se

esttab Baseline CubicNoInter Linear LinNoInter using "$path\SummaryStatistics\tableA3.txt", replace compress keep(country distance_border distance_border2 distance_border3 interact interact2 interact3) ///
coeflabels(country "Germany" distance_border "alfa1" distance_border2 "alfa2" distance_border3 "alfa3" interact "eta1" interact2 "eta2" interact3 "eta3") ///
s(N r2) nonumbers nodepvars se

erase tempRDD.dta
